package com.wyx.Controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wyx.Common.R;
import com.wyx.DTO.OrdersDto;
import com.wyx.Pojo.OrderDetail;
import com.wyx.Pojo.Orders;
import com.wyx.Pojo.User;
import com.wyx.Service.OrderDetailService;
import com.wyx.Service.OrdersService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrdersService ordersService;
    @Autowired
    private OrderDetailService orderDetailService;

    /**
     * 提交订单
     * @param orders
     * @param session
     * @return
     */
    @PostMapping("/submit")
    public R<String> addneworder(@RequestBody Orders orders, HttpSession session){
        if (orders.getAddressBookId()==null){
            return R.error("请输入地址");
        }
        boolean submit = ordersService.submit(orders, session);
        if (!submit){
            return R.error("订单价格或者状态发生改变，请重新下单");
        }
        return R.success("下单成功！");
    }

    /**
     * 订单查询
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/userPage")
    public R<Page> getorders(int page, int pageSize,HttpSession session){
        //获得当前用户的id
//        Long currendId = Basecontext.getCurrendId();
        User user = (User) session.getAttribute("user");
        LambdaQueryWrapper<Orders>wrapper=new LambdaQueryWrapper<>();

        wrapper.eq(Orders::getUserId,user.getId());
        wrapper.orderByAsc(Orders::getOrderTime);

        Page<Orders>page1=new Page<>(page,pageSize);
        //page1有值了
        ordersService.page(page1, wrapper);

        Page<OrdersDto>res=new Page<>();

        BeanUtils.copyProperties(page1,res);

        List<OrdersDto>list=new ArrayList<>();

        List<Orders> list2 = ordersService.list(wrapper);

        for (Orders orders : list2) {
            OrdersDto ordersDto1=new OrdersDto();
            BeanUtils.copyProperties(orders,ordersDto1);
            LambdaQueryWrapper<OrderDetail>wrapper1=new LambdaQueryWrapper<>();
            wrapper1.eq(OrderDetail::getOrderId,orders.getId());
            List<OrderDetail> list1 = orderDetailService.list(wrapper1);
            ordersDto1.setOrderDetails(list1);
            list.add(ordersDto1);
        }
        res.setRecords(list);

        return R.success(res);
    }
    //
    @GetMapping("/page")
    public R<Page> backendOrderPage(int page , int pageSize, String number,
                                    String beginTime,String endTime){

        LambdaQueryWrapper<Orders>wrapper=new LambdaQueryWrapper<>();
        if (beginTime!=null){
        DateTimeFormatter dtf=DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime bt= LocalDateTime.parse(beginTime,dtf);
        LocalDateTime et= LocalDateTime.parse(endTime,dtf);
        wrapper.in(Orders::getOrderTime,bt,et);
        }
        wrapper.like(number!=null,Orders::getNumber,number);
        Page<Orders>pageorder=new Page<>(page,pageSize);
        Page<Orders> page1 = ordersService.page(pageorder, wrapper);
        return R.success(page1);
    }
}
